using System;
using L=Science.Physics.GeneralPhysics;

namespace Serway.Chapter05
{
	/// <summary>
	/// Example09: The Atwood Machine 
	/// When two objects of unequal mass are hung vertically
	/// over a frictionless pulley of negligible mass, the 
	/// arrangement is called an Atwood machine. The device is
	/// sometimes used in the laboratory to measure the free-fall
	/// acceleration. Determine the magnitude of the acceleration
	/// of the two objects and the tension in the lightweight cord.
	/// a_y = (m_2-m_1)/(m_1+m_2)*g
	/// T = 2*m_1*m_2/(m_1+m_2)*g
	/// </summary>
	public class Example09
	{
		public Example09()
		{
		}
		private string result;
		public string Result
		{
			get{return result;}
		}
		public void Compute()  
		{
			L.TotalForce[] f = new L.TotalForce[2]; 
			L.Mass[] m = new L.Mass[2];
			L.Acceleration[] a = new L.Acceleration[2];

			m[0]= new L.Mass();
			m[0].kg = 10.0;
			m[1]= new L.Mass();
			m[1].kg = 20.0;
			
			L.Force[] ff = new L.Force[2];
			ff[0] = new L.Force();
			ff[0].YVariableQ = true;
			ff[1] = new L.Force();
			ff[1].Y = -m[0].kg*L.Constant.AccelerationOfGravity;
			f[0] = new L.TotalForce(ff);
			
			L.Force[] sf = new L.Force[2];
			sf[0] = new L.Force();
			sf[1] = new L.Force();
			sf[0].YVariableQ = true;
			sf[1].Y = -m[1].kg*L.Constant.AccelerationOfGravity;
			f[1] = new L.TotalForce(sf);			
			
			a[0] = new L.Acceleration();
			a[0].YVariableQ = true; 
			a[1] = new L.Acceleration();
			a[1].YVariableQ = true; 

			L.NewtonEquation.ConstraintFunctionToBeZero[] cf
				= new L.NewtonEquation.ConstraintFunctionToBeZero[2];
			cf[0] = new L.NewtonEquation.ConstraintFunctionToBeZero(Min0);
			cf[1] = new L.NewtonEquation.ConstraintFunctionToBeZero(Min1);
			
			L.NewtonEquation eq = new L.NewtonEquation(f,m,a);			
			eq.Constraint(cf);

			eq.Solve();
		
			result += Convert.ToString(a[0].Y)+"\r\n";
			result += Convert.ToString(f[1].DecomposedForce[0].Y)+"\r\n";    
		
			result += Convert.ToString((m[1].kg-m[0].kg)/(m[0].kg+m[1].kg)
				*L.Constant.AccelerationOfGravity)+"\r\n";
			result += Convert.ToString(2.0*m[1].kg*m[0].kg/(m[0].kg+m[1].kg)
				*L.Constant.AccelerationOfGravity)+"\r\n";
		}
		private double Min0(L.TotalForce[] f, L.Mass[] m, L.Acceleration[] a)
		{
			return f[0].DecomposedForce[0].Y - f[1].DecomposedForce[0].Y;
		}
		private double Min1(L.TotalForce[] f, L.Mass[] m, L.Acceleration[] a)
		{
			return a[0].Y + a[1].Y;
		}
	}
}
